package com.graduate.daydayup.activity

import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.util.Log
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.ToastUtils
import com.google.gson.Gson
import com.google.gson.JsonObject
import com.google.gson.reflect.TypeToken
import com.graduate.daydayup.R
import com.graduate.daydayup.adpter.HotNewsAdapter
import com.graduate.daydayup.base.BaseActivity
import com.graduate.daydayup.http.Api
import com.graduate.daydayup.http.ApiService
import com.graduate.daydayup.http.HotBean
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Call
import retrofit2.Response
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import  com.graduate.daydayup.http.Result
import kotlinx.android.synthetic.main.activity_hotnews.*
import org.json.JSONObject

class HotNewsActivity:BaseActivity() {
    val adapter=HotNewsAdapter(R.layout.layout_hotnews)

    override fun setTitle()="微博热搜"

    override fun getLayout()= R.layout.activity_hotnews

    override fun setTextRight()=""

    override fun setImgRightListener() {

    }

    override fun isAddToolbar()=true

    override fun initViewIds() {

    }

    override fun initView() {
        rv_hotnews.adapter=adapter
        rv_hotnews.layoutManager=LinearLayoutManager(this)
        adapter.setOnItemClickListener { adapter, view, position ->
            val hotbean=adapter.getItem(position) as HotBean
            val url="https://s.weibo.com/weibo?q=%23${hotbean.hotword}%23&Refer=new_time"
            ActivityUtils.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(url)))

        }
    }

    override fun initData(savedInstanceState: Bundle?) {
        //日志拦截器
        var httpLoggingInterceptor = HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY)
        //Okhttp对象
        var okHttpClient = OkHttpClient.Builder()
                .addInterceptor(httpLoggingInterceptor)
                .build()
        //创建Retrofit对象
        var retrofit = Retrofit.Builder().baseUrl(Api.BASE_URL)
                .client(okHttpClient)
                .addConverterFactory(GsonConverterFactory.create()).build()
        //创建请求接口类
        var userApiService = retrofit.create(ApiService::class.java)
        userApiService.login(Api.LOGIN_URL,"c249efe5fda5d4460d35dbde43c7c098").enqueue(object : retrofit2.Callback<Result>{
            override fun onFailure(call: Call<Result>, t: Throwable) {

                Log.e("test", t.toString())

            }

            override fun onResponse(call: Call<Result>, response: Response<Result>) {
                val newslist = response.body()?.newslist
                if (newslist!=null){
                    adapter.setList(newslist)
                    adapter.notifyDataSetChanged()

                }



            }
        })
    }
}